package cn.chenxiejia;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: T8
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/2/8 10:46
 */
public class T8 {
    static String[] s = new String[]{"", "+", "-"};
    static Random rand = new Random();
    static StringBuilder ss = new StringBuilder();
    static CountDownLatch latch = new CountDownLatch(1);
    static List<Long> list = new ArrayList<>();
    static List<Long> list1 = new ArrayList<>();
    static Long a;

    public static void main(String[] args) {
        T8 t = new T8();
        for (int i = 0; i < 11; i++) {
            new Thread(t::num, "T" + i).start();
        }
        t.num();
        try {
            TimeUnit.SECONDS.sleep(1);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("计算完成。。。");

    }

    void num() {
        if (list1.size() < 1) {
            Pattern p = Pattern.compile("(\\d+|-\\d+)");

            while (true) {

                for (int i = 1; i < 9; i++) {
                    ss.append(i + s[rand.nextInt(3)]);
                }
                ss.append(9);
                Matcher m = p.matcher(ss.toString());

                while (m.find()) {
                    a = Long.parseLong(m.group());
                    list.add(a);
                }
                //System.out.println(list);
                //System.out.println(list.stream().reduce(0L, Long::sum));
                if (list.stream().reduce(0L, Long::sum) == 100) {
                    System.out.printf("%s:%s=100%n", Thread.currentThread().getName(), ss);
                    list1.add(a);
                    break;
                }
                ss.delete(0, ss.length());
                list.clear();


            }
        }
    }
}
